package cn.itcast.nems.product.service.impl;

import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.product.dao.dataobject.ProductPriceRuleDO;
import cn.itcast.nems.product.dao.entity.ProductPriceRule;
import cn.itcast.nems.product.dao.mapper.ProductPriceRuleMapper;
import cn.itcast.nems.product.service.ProductPriceRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品定价规则 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2023-08-31
 */
@Service
public class ProductPriceRuleServiceImpl extends ServiceImpl<ProductPriceRuleMapper, ProductPriceRule> implements ProductPriceRuleService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ProductPriceRuleMapper productPriceRuleMapper;

    public ProductPriceRuleServiceImpl(ProductPriceRuleMapper productPriceRuleMapper) {
        this.productPriceRuleMapper = productPriceRuleMapper;
    }

    @Override
    public void updateProductPriceRule(List<ProductPriceRule> productPriceRules) {
        if (productPriceRules == null || productPriceRules.isEmpty()) {
            return;
        }
        logger.info("productPriceRules:{}", productPriceRules);
        final Set<Long> productIds = productPriceRules
                .stream()
                .map(ProductPriceRule::getProductId)
                .collect(Collectors.toSet());
        this.productPriceRuleMapper.deleteByProductIds(productIds, AccountHolder.getRequiredAccount());
        productPriceRules.forEach(r -> {
            r.setTenantId(TenantInfo.DEFAULT_TENANT_ID);
            r.setSubtenantId(TenantInfo.DEFAULT_SUBTENANT_ID);
            r.setDeletedFlag(false);
            r.setCreatedBy(AccountHolder.getRequiredAccount());
            r.setCreatedDatetime(LocalDateTime.now());
            r.setModifiedDatetime(LocalDateTime.now());
        });
        this.productPriceRuleMapper.insertBatchSomeColumn(productPriceRules);
    }

    @Override
    public ProductPriceRule getCurrentPriceRuleByProduct(Long productId) {
        return this.productPriceRuleMapper.selectCurrentPriceRule(productId);
    }

    @Override
    public Map<Long, ProductPriceRule> findPriceRuleByProducts(Set<Long> productIds) {
        Map<Long, ProductPriceRule> result;
        if (productIds == null || productIds.isEmpty()) {
            result = Collections.emptyMap();
        } else {
            final List<ProductPriceRule> rules = this.productPriceRuleMapper.selectCurrentPriceRules(productIds);
            result = rules.stream().collect(Collectors.toMap(ProductPriceRule::getProductId, p -> p));
        }
        return result;
    }

    @Override
    public ProductPriceRuleDO findPriceRuleByBizNo(String bizNo, LocalDateTime useDateTime){
        return this.productPriceRuleMapper.selectPriceRuleByBizNo(bizNo, useDateTime);
    }
}
